﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;

namespace Se.Net.Sockets
{
    public class SocketConnection
    {
        private Socket _socket;
        private SocketAsyncEventArgs _sendArgs;
        private SocketAsyncEventArgs _receivedArgs;
        private byte[] bt = new byte[10000];

        #region Event

        /// <summary>
        /// 接收数据事件
        /// </summary>
        public event Action<Socket> ReceiveDataEvent;
        /// <summary>
        /// 触发接收数据事件
        /// </summary>
        /// <param name="socket"></param>
        public void OnReceiveData(Socket socket)
        {
            if (ReceiveDataEvent != null)
                ReceiveDataEvent(socket);
        }
        /// <summary>
        /// 发送数据后事件
        /// </summary>
        public event Action<Socket, string> SendDataEvent;
        /// <summary>
        /// 触发发送数据事件
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="message"></param>
        public void OnSendData(Socket socket, string message)
        {
            if (this.SendDataEvent != null) this.SendDataEvent(socket, message);
        }

        #endregion

        public SocketConnection(Socket socket)
        { 
            this._socket = socket;
            this._receivedArgs = new SocketAsyncEventArgs();
            this._sendArgs = new SocketAsyncEventArgs();
            this._receivedArgs.Completed += ReceivedArgs_Completed;
            this._sendArgs.Completed += SendArgs_Completed;
            this._receivedArgs.AcceptSocket = this._socket;
            this._sendArgs.AcceptSocket = this._socket;
            this._receivedArgs.SetBuffer(bt, 0, 10000);
            this._sendArgs.SetBuffer(bt, 0, 10000);
        }


        public void Receive()
        {
            Console.WriteLine("开始接收数据");
            this._socket.ReceiveAsync(this._receivedArgs);
        }

        public void Send(string message)
        {
            var sendBytes = Encoding.UTF8.GetBytes(message);
            this._sendArgs.SetBuffer(sendBytes, 0, sendBytes.Length);
            this._socket.SendAsync(this._sendArgs);
        }


        /// <summary>
        /// Socket发送数据完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SendArgs_Completed(object sender, SocketAsyncEventArgs e)
        {
            
        }

        /// <summary>
        /// Socket 接受数据完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ReceivedArgs_Completed(object sender, SocketAsyncEventArgs e)
        {
            var buffer = new ArraySegment<byte>(e.Buffer, 0, e.BytesTransferred);
            string message = Encoding.UTF8.GetString(e.Buffer, 0, e.BytesTransferred);

            Console.WriteLine("接受到数据：{0}", message);
        }


    }
}
